package com.itjiange;

/**
 * 中介者
 *
 * @author ItJianGe
 * @data 2024/8/22 10:29
 */
public class Mediator {

    /**
     * 采购人员
     */
    protected Purchase purchase;

    /**
     * 销售人员
     */
    protected Sale sale;

    /**
     * 库存管理人员
     */
    protected Stock stock;

    public Mediator() {
        purchase = new Purchase(this);
        sale = new Sale(this);
        stock = new Stock(this);
    }

    /**
     * 采购电脑
     *
     * @param number
     */
    public void purchaseByComputer(int number) {
        // 1. 先获取销售情况
        int saleStatue = sale.getSaleStatue();
        // 2. 根据销售情况判断采购数量
        // 2.1. 如果销售数量 <= 80 则减半采购
        String msg = "正常采购，";
        if (saleStatue <= 80) {
            number /= 2;
            msg = "数量减半采购，";
        }
        System.out.println(msg + "当前采购数量为：" + number);
        // 3. 增加库存
        stock.increaseComputer(number);
    }

    /**
     * 销售电脑
     * @param number
     */
    public void saleComputer(int number) {
        // 1. 获取库存
        int stockNumber = stock.getStockNumber();
        // 2. 如果销售数量大于等于库存则需要进行采购
        if (number >= stockNumber) {
            purchase.purchaseComputer(number);
        }
        // 3. 减少库存
        stock.decreaseComputer(number);
    }

    /**
     * 折价销售电脑
     */
    public void discountSale() {
        sale.discountSale();
    }

    /**
     * 清理库存
     */
    public void stockClear() {
        sale.discountSale();
        purchase.stopPurchasingComputer();
    }
}
